package sunbao.ido.support;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * \* User: MeiZhongHao
 * \* Date: 2018-10-23
 * \* Time: 10:14
 * \* Description: 通过回调，加载IDo bean
 * \
 */
@Configuration
public class IDoServiceBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {

    private DefaultListableBeanFactory acf;

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
//        Reflections reflections = new Reflections("com.sunbao.service");
//        Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(IDo.class);
//        for (Class<?> serviceClass : annotated) {
//            for (Annotation annotation : serviceClass.getAnnotations()) {
//                BeanDefinitionBuilder bdb;
//                if (annotation instanceof IDo) {
//                    bdb = BeanDefinitionBuilder.rootBeanDefinition(IDoFactoryBean.class.getName());
//                    bdb.getBeanDefinition().setAttribute("id", serviceClass.getName());
//                    bdb.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(serviceClass.getName());
//                    acf.registerBeanDefinition(serviceClass.getName(), bdb.getBeanDefinition());
//                }
//            }
//        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.acf = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
    }
}